/*本模块功能:我的信息(学生)
        编写者:朱光曜
        编写时间:2017.11.03*/
$(function() {
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });
    document.addEventListener('DOMSubtreeModified',function(e){//去除空格
        $(".publicText").attr("maxlength","20");


        $.map($(".publicText"),function (opt) {//长度限制
            maxLengthLimit.init(opt);
        });
        $(".publicText").bind('input propertychange',function(){

            var regy = /\s/g;
            if(regy.test($(this).val())){
                $(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
            }
        });
    });
    var box = new  Alert;
    var string1 = '<tr>' +
        '<th class="col-lg-2">序号</th>' +
        '<th class="col-lg-8">教师</th>' +
        '<th class="col-lg-6">课程</th>' +
        /*'<th class="col-lg-5">课程进度</th>' +*/
        '</tr>';

    function myMessage(message) {//我的信息

        if(message.memberList[0].sexName == "男") {
            var sexList = '<input type="radio" checked="checked" name="sex" id="man" value=""/><label for="man" class="checked" userSex="m">男</label> <input type="radio" name="sex" id="woman" value=""/><label for="woman" userSex="f">女</label>';
        }
        if(message.memberList[0].sexName == "女") {
            var sexList = '<input type="radio"  name="sex" id="man" value=""/><label for="man" userSex="m">男</label> <input type="radio" checked="checked"  name="sex" id="woman" value=""/><label for="woman" class="checked" userSex="f">女</label>';
        }
        return '<div class="fl inputRight"><label for="departName" class="fl">学号:&ensp;</label><div id="studentID"   class="fl" title="'+message.memberList[0].stuorteaId +'">'+message.memberList[0].stuorteaId +'</div></div>' +
            '<div class="fr inputRight"><label for="departName" class="fl">姓名:&ensp;</label><div    class="fl" title="'+message.memberList[0].userName +'">'+message.memberList[0].userName +'</div></div>' +
            '<div class="fl inputRight"><label for="departName" class="fl" departmentId="'+message.memberList[0].departmentId +'">院系:&ensp;</label><div id="department"   class="fl" title="'+message.memberList[0].departmentName +'">'+message.memberList[0].departmentName +'</div></div>' +
            '<div class="fr inputRight"><label for="departName" class="fl" professionId="'+message.memberList[0].professionId +'">专业:&ensp;</label><div id="major"   class="fl" title="'+message.memberList[0].professionName +'">'+message.memberList[0].professionName +'</div></div>' +
            '<div class="fl inputRight"><label for="departName" class="fl">班级:&ensp;</label><div id="class"   class="fl" title="'+message.memberList[0].className +'">'+message.memberList[0].className +'</div></div>' +
            '<div class="fr inputRight"><label for="departName">电话:&ensp;</label><input type="text" id="phone" class="publicText" value="'+message.memberList[0].telephoneNum +'"/></div>' +
            '<div class="fl sex inputRight" id="sex"><span>性别:&ensp;</span>' +
            sexList+
            '</div>' +
            '<div class="clear"></div>';
    }

    function tabelModel(data) {//列表内容

        return '<tr data-num="0">' +
            '<td class="col-lg-2">'+(parseInt(key)+1) +'</td>' +
            '<td class="col-lg-8">'+data.courseList[key].userName+'</td>' +
            '<td class="col-lg-6">'+data.courseList[key].courseName+'</td>' +
            /*'<td class="col-lg-5">'+data.courseList[key].courseProcess+'</td>' +*/
            '</tr>';

    }
    var nowData = 1;
    var index = {

        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10,
                "param": 3,
                "roleId": 1,
                "selecetRoleId": 1,
                "stuorteaId":result.stuorteaId
            };

            this.postData(this.parameter, nowData);

            $(document).off("click","#sex input").on("click","#sex input",function(){//点击切换性别

                $(this).next("label").addClass("checked").siblings("label").removeClass("checked");
            });


        },
        add: function() { //搜索追加
            var self = this;
            var data = self.data_content;

            $(".funBox").html(myMessage(data));

            $(".preservation").off("click").click(function() { //保存点击
                self.keep();
            });

        },
        tab_add: function() { //列表追加
            var self = this;
            var data = self.tab_content;
            var table = string1;

            //data. courseList={};

            var i = 0;
            for(key in data.courseList) {
                i++;
            }

            if(i > 0) {

                for(key in data.courseList) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);
            } else {
                var trImg = "<tr><td colspan='3'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
                $(".mainContent .tableBox").html(string1+ trImg);
            }
            //设置列表宽度(最大为9)
            var numMax = 0;
            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));
                numMax += num1;
            });

            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));

                $(this).css("width", num1 / numMax * 100 + "%");
            });
        },
        postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "getMemberDetailById",
                async: true,
                data:{
                    "jsonStr":	JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);

                    if(hunt) {
                        self.tab_content = new_data;

                        self.tab_add();
                    } else {

                        self.data_content = new_data;
                        self.tab_content = new_data;

                        self.add();
                        self.tab_add();
                    }

                    var sumpage = new_data.totalPage;
                    if(sumpage<1){sumpage=1;};
                    var nowpage = 1;
                    var pagenumber = 10;
                    var parame = parameter;
                    $(".tcdPageCode").createPage({
                        pageCount: sumpage, //总页数
                        current: nowpage, //当前页数
                        pageNumber: pagenumber, //每页显示数量
                        backFn: function(p) {

                            nowpage = p;

                            if(nowpage >= sumpage) {
                                nowpage = sumpage;
                            }
                            if(!parameter) {
                                parameter = {
                                    "pageNow": nowpage,
                                    "pageSize": 10
                                };

                            } else {
                                parameter.pageNow = nowpage;

                            }
                            $.ajax({
                                type: "post",
                                url: "getMemberDetailById",
                                async: true,
                                data:{
                                    "jsonStr":	JSON.stringify(parameter)
                                },
                                async: true,
                                success: function(msg) {
                                    //获取院系
                                    var msgNew = JSON.parse(msg);
                                    self.tab_content = msgNew;

                                    self.tab_add();
                                },
                                error: function(data, status, xhr) {
                                    var json = JSON.stringify({
                                        "pageName": "error"
                                    });
                                    window.location.href = "skipPage?jsonStr=" + json;

                                }

                            });
                        }
                    });
                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        keep:function(){//保存
            var self=this;
            var telephoneNum=$("#phone").val();
            var userSex=$(".checked").attr("usersex");
            var n = /^[1-9]\d*$|^0$/;

            // if(!n.test(telephoneNum)&&telephoneNum.length!==0) {
            //     self.alert1("请输入数字信息");
            //     return;
            // }else if(telephoneNum.length!==11&&telephoneNum.length!==0){
            //     self.alert1("请输入11位数字信息");
            //     return;
            // }else if(verification.telphoneCode(telephoneNum) !== true) {
            //     self.alert1(verification.telphoneCode(telephoneNum));
            //     return;
            // }
            var thisData = {
                "param": 1,
                "telephoneNum": telephoneNum,
                "userSex":userSex,
                "userContect": "",
            };

            $.ajax({
                type: "post",
                url: "saveMyInfo",
                async: true,

                data: {
                    "jsonStr": JSON.stringify(thisData)
                },
                async: true,
                success: function(msg) {
                    var msgNew = JSON.parse(msg);

                    if(msgNew.flag) {
                        self.alert1("保存成功");
                    } else {
                        //self.alert1(msgNew.msg);
                        if(msgNew.msg){
                            self.alert1(msgNew.msg);
                        }else{
                            self.alert1("保存失败");
                        }


                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        alert1: function(content2, method) { //alert提示

            box.contenthead = "提示";
            box.content = content2;
            if(method) {
                box.success = method;
            }
            box.init();
            box.show();
        }

    };

    index.init();

});